package com.gu.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.gu.config.VodProperties;
import com.gu.service.EduVideoUploadService;
import com.boot.exception.DiyException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 05-27-15:35
 */
@Service
public class EduVideoUploadServiceImpl implements EduVideoUploadService {

    @Resource
    private VodProperties vodProperties;

    //初始化上传工具类方法
    private static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    //上传视频到阿里云
    @Override
    public String upload(MultipartFile file) {
        //对字符串文件名字进行截取；
        String fileName = file.getOriginalFilename();
        String title = (String) fileName.subSequence(0, fileName.lastIndexOf("."));

        String accessKeyId = vodProperties.getKeyId();
        String accessKeySecret = vodProperties.getKeySecret();

        //获取文件流：
        String videoId = null;
        try {
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                throw new DiyException("444", "上传失败！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoId;
    }

    //根据视频Id来删除视频
    @Override
    public void deleteVideoByVideoId(String videoId) {
        String accessKeyId = vodProperties.getKeyId();
        String accessKeySecret = vodProperties.getKeySecret();
        try {
            DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            //获取删除响应
            DeleteVideoResponse response = client.getAcsResponse(request);

        } catch (ClientException e) {
            e.printStackTrace();

        }
    }

    /**
     * 前台根据videoId来获取视频的播放凭证
     */
    @Override
    public String getPlayAuth(String videoId) {
        String accessKeyId = vodProperties.getKeyId();
        String accessKeySecret = vodProperties.getKeySecret();

        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        String playAuth = "";
        try {
            //密钥登陆验证
            DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
            //获取视频播放凭证初始化
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);
            response = client.getAcsResponse(request);
            //播放凭证
            playAuth = response.getPlayAuth();
            System.out.print("PlayAuth = " + playAuth + "\n");
            //VideoMeta信息
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
        return playAuth;
    }
}
